package com.elective.courseselect.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.elective.courseselect.entity.AnswerEntity;
import com.elective.courseselect.entity.AnswerListVo;
import com.elective.courseselect.entity.CourseQuestionnaireEntity;
import com.elective.courseselect.entity.InputAnswerListVo;
import com.elective.courseselect.service.AnswerService;
import com.elective.courseselect.service.CourseQuestionnaireService;
import com.elective.courseselect.utils.PageUtils;
import com.elective.courseselect.utils.R;
import com.elective.status.StatusCode;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;



/**
 * 回答问卷表
 *
 * @author lee
 * @email 943204006@qq.ccom
 * @date 2021-09-04 19:23:06
 */
@RestController
@RequestMapping("courseselect/answer")
public class AnswerController {
    @Autowired
    private AnswerService answerService;

    @Autowired
    private CourseQuestionnaireService courseQuestionnaireService;

    /**
     * 列表
     */
    @GetMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = answerService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id){
		AnswerEntity answer = answerService.getById(id);

        return R.ok().put("answer", answer);
    }

    /**
     * 保存
     */
    @PostMapping(value = "/save")
    public R save(@RequestBody AnswerEntity answer){
		answerService.save(answer);

        return R.ok();
    }

    /**
     * 修改
     */
    @PostMapping("/update")
    public R update(@RequestBody AnswerEntity answer){
		answerService.updateById(answer);
        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping("/delete")
    public R delete(@RequestBody Integer[] ids){
		answerService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }


    //填写性格调查问卷
    @ApiOperation("填写性格调查问卷")
    @PostMapping(value = "/inputPersonalityQuestionnaire")
    public R inputPersonalityQuestionnaire(@RequestBody InputAnswerListVo inputAnswerListVo) {
        R r =answerService.inputPersonalityQuestionnaire(inputAnswerListVo);
        return r;
    }


    //填写课程意愿调查问卷
    @ApiOperation("填写课程意愿调查问卷")
    @PostMapping(value = "/inputClassQuestionnaire")
    public R inputClassQuestionnaire(@RequestBody InputAnswerListVo inputAnswerListVo) {
        QueryWrapper<CourseQuestionnaireEntity> coursequestionnaireEntityQuery = new QueryWrapper();
        coursequestionnaireEntityQuery.eq("course_id", inputAnswerListVo.getCourseId());

        CourseQuestionnaireEntity courseQuestionnaireEntity = courseQuestionnaireService.getOne(coursequestionnaireEntityQuery);
        inputAnswerListVo.setQuestionnaireId(courseQuestionnaireEntity.getQuestionnaireId());
        R r = answerService.inputClassQuestionnaire(inputAnswerListVo);
        return r;
    }

    //查询学生答题列表
    @ApiOperation("查询学生答题列表")
    @GetMapping(value = "/searchStuAnswerListByQuestionnaireId/{questionnaireId}")
    public R searchStuAnswerListByQuestionnaireId(@PathVariable("questionnaireId") Integer questionnaireId) {
        List<AnswerEntity> list =  answerService.searchStuAnswerListByQuestionnaireId(questionnaireId);
        return R.ok(StatusCode.OK,"查询学生答题列表成功").put("list", list);
    }



    @ApiOperation("查询学生答题详情")
    @GetMapping(value = "/searchStuAnswerDetails/{questionnaireId}/{userId}")
    public R searchStuAnswerDetails(@PathVariable("questionnaireId") Integer questionnaireId,
                                                  @PathVariable("userId") Integer userId) {
        AnswerListVo list =  answerService.searchStuAnswerDetails(questionnaireId,userId);
        return R.ok(StatusCode.OK,"查询学生答题详情成功").put("list", list);
    }

}
